import 'package:flutter/material.dart';
import 'package:flutter_lui/flutter_lui.dart';
import 'package:flutter_lui/src/l_style.dart';

class LRadio extends StatelessWidget {
  /// 值
  final bool? value;

  /// 值改变时的回调
  final Function(bool value)? onChange;

  /// 是否禁用
  final bool? disabled;

  /// 类型 根据类型显示颜色 优先级没有color高
  final LUIType? type;

  /// 颜色
  final Color? color;

  /// 大小
  final double? size;

  /// 外边距
  /// - [1]=>all
  /// - [1,1]=>[上下，右左]
  /// - [1,1,1]=> [上，右左，下]
  /// - [1,1,1,1]=> [上，右，下，左]
  final List<double> margin;

  /// 左外边距
  final double? marginLeft;

  /// 右外边距
  final double? marginRight;

  /// 上外边距
  final double? marginTop;

  /// 下外边距
  final double? marginBottom;

  /// 上下外边距
  final double? marginVertical;

  /// 左右外边距
  final double? marginHorizontal;
  const LRadio(
      {this.value,
      this.onChange,
      this.disabled,
      this.type,
      this.color,
      this.size,
      this.margin = const [0],
      this.marginLeft,
      this.marginRight,
      this.marginTop,
      this.marginBottom,
      this.marginVertical,
      this.marginHorizontal,
      Key? key})
      : super(key: key);

  handleChange() {
    if (onChange != null) {
      onChange!(value == false);
    }
  }

  @override
  Widget build(BuildContext context) {
    var useColor = color ??
        (type == LUIType.danger
            ? LUI.instance.dangerColor
            : type == LUIType.success
                ? LUI.instance.successColor
                : type == LUIType.warning
                    ? LUI.instance.warningColor
                    : LUI.instance.primaryColor);
    Widget view = GestureDetector(
      onTap: disabled == true ? null : handleChange,
      child: Icon(
        value == true ? Icons.check_circle : Icons.circle_outlined,
        color: useColor.withOpacity(disabled == true ? 0.5 : 1),
        size: size ?? lrpx(20),
      ),
    );
    var marginValue = findTRBL(margin);
    var mt = marginTop ?? marginVertical ?? marginValue[0] ?? 0;
    var mr = marginRight ?? marginHorizontal ?? marginValue[1] ?? 0;
    var mb = marginBottom ?? marginVertical ?? marginValue[2] ?? 0;
    var ml = marginLeft ?? marginHorizontal ?? marginValue[3] ?? 0;
    if (mt != 0 || mr != 0 || mb != 0 || ml != 0) {
      view = Padding(
          padding: EdgeInsets.only(top: mt, right: mr, bottom: mb, left: ml),
          child: view);
    }
    return view;
  }
}
